<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">

<!--      <el-form-item label="状态" prop="state">-->
<!--        <el-input-->
<!--          v-model="queryParams.state"-->
<!--          placeholder="请输入状态"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="" prop="state">
        <el-select style="width: 190px" v-model="queryParams.state" placeholder="请选择状态" clearable @keyup.enter="handleQuery">
          <el-option
              v-for="(item, index) in allState"
              :key="index"
              :label="item.label"
              :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="" style="width: 308px;">
        <el-date-picker
            v-model="dateRange"
            value-format="YYYY-MM-DD"
            type="daterange"
            range-separator="-"
            start-placeholder="申请时间"
            end-placeholder="结束时间"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

<!--    <el-row :gutter="10" class="mb8">-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="Plus"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['system:applicationson:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="Edit"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['system:applicationson:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="Delete"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['system:applicationson:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="Download"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['system:applicationson:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->

    <el-table v-loading="loading" :data="applicationsonList" @selection-change="handleSelectionChange">

<!--      <el-table-column label="合伙人申请ID" align="center" prop="partnerId" />-->
      <el-table-column label="邀请ID" align="center" prop="inviteId" />
      <el-table-column label="所属区域" align="center" prop="location" />
      <el-table-column label="姓名" align="center" prop="userName" />
      <el-table-column label="联系电话" align="center" prop="phone" />
      <el-table-column label="申请人ID" align="center" prop="applicationId" />
      <el-table-column label="申请人角色" align="center" prop="applicationRole" />
      <el-table-column label="申请时间" align="center" prop="applicationTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.applicationTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="状态" align="center" prop="state" />-->
      <el-table-column
          label="状态"
          align="center"
          prop="state">
        <template #default="scope">
          <el-tag v-if="scope.row.state == 0" type="primary">待审批</el-tag>
          <el-tag v-if="scope.row.state == 1" type="success">通过</el-tag>
          <el-tag v-if="scope.row.state == 2" type="danger">驳回</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="处理时间" align="center" prop="dealTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.dealTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="处理详情" align="center" prop="dealDetails" />

      <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width"  fixed="right">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" v-if="scope.row.state == 0" @click="handleUpdate(scope.row),isShow(true)"

                     v-hasPermi="['system:applicationson:edit']">处理
          </el-button>
          <el-button link type="primary" icon="View" v-if="scope.row.state == 1" @click="handleUpdate(scope.row),isShow(false)"
                     v-hasPermi="['system:applicationson:view']">查看
          </el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['system:applicationson:remove']">删除
          </el-button>
        </template>
      </el-table-column>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改合伙人申请对话框 -->
    <el-dialog :title="title" v-model="open" width="650px" append-to-body>
      <el-form ref="applicationsonRef" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="邀请人ID" prop="inviteId">
          <el-input v-model="form.inviteId" placeholder="请输入邀请人ID" />
        </el-form-item>
        <el-form-item label="所属区域ID" prop="zoneId">
          <el-input v-model="form.location" placeholder="请输入所属区域ID" />
        </el-form-item>
        <el-form-item label="姓名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>

<!--        <el-form-item label="状态" prop="state">-->
<!--          <el-input v-model="form.state" placeholder="请输入状态" />-->
<!--        </el-form-item>-->
        <el-form-item
            label="状态"
            prop="state"
        >
          <el-radio-group v-model="form.state">
<!--            <el-radio label="0">待审批</el-radio>-->
            <el-radio label="1">申请成功</el-radio>
            <el-radio label="2">申请失败</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="处理详情" prop="dealDetails">
          <el-input v-model="form.dealDetails" type="textarea" :rows="3" placeholder="请输入处理详情" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer" >
          <el-button type="primary" @click="submitForm" v-if="flag===1">确 定</el-button>
          <el-button @click="cancel" v-if="flag===1">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Applicationson">
import { listApplicationson, getApplicationson, delApplicationson, addApplicationson, updateApplicationson } from "@/api/system/applicationson";

const { proxy } = getCurrentInstance();
const flag = ref(0);
const applicationsonList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    applicationTime: null,
    state: null,
  },
  rules: {
    inviteId: [
      { required: true, message: "邀请人ID不能为空", trigger: "blur" }
    ],
    location: [
      { required: true, message: "所属区域ID不能为空", trigger: "blur" }
    ],
    applicationTime: [
      { required: true, message: "申请时间不能为空", trigger: "blur" }
    ],
    state: [
      { required: true, message: "状态不能为空", trigger: "blur" }
    ],
    dealTime: [
      { required: true, message: "处理时间不能为空", trigger: "blur" }
    ],
    dealDetails: [
      { required: true, message: "处理详情不能为空", trigger: "blur" }
    ],
  },
  allState: [
    {
      label: "待审批",
      value: 0,
    },
    {
      label: "通过",
      value: 1,
    },
    {
      label: "驳回",
      value: 2,
    },
  ],
});

const { queryParams, form, rules, allState } = toRefs(data);
//是否能继续修改
const isShow = (flagStatus) => {
  if(flagStatus){
    flag.value=1;
  }else{
    flag.value=0;
  }
}
/** 查询合伙人申请列表 */
function getList() {
  loading.value = true;
  listApplicationson(proxy.addDateRange(queryParams.value,dateRange.value)).then(response => {
    applicationsonList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    partnerId: null,
    inviteId: null,
    zoneId: null,
    applicationId: null,
    applicationRole: null,
    applicationTime: null,
    state: null,
    dealTime: null,
    dealDetails: null
  };
  proxy.resetForm("applicationsonRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  dateRange.value = [];
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.partnerId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加合伙人申请";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _partnerId = row.partnerId || ids.value
  getApplicationson(_partnerId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改合伙人申请";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["applicationsonRef"].validate(valid => {
    if (valid) {
      if (form.value.partnerId != null) {
        updateApplicationson(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addApplicationson(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _partnerIds = row.partnerId || ids.value;
  proxy.$modal.confirm('是否确认删除合伙人申请编号为"' + _partnerIds + '"的数据项？').then(function() {
    return delApplicationson(_partnerIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/applicationson/export', {
    ...queryParams.value
  }, `applicationson_${new Date().getTime()}.xlsx`)
}

getList();
</script>
